<?php ob_start();?>
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/checkUser.php');
$db = connect();
$menuId = $_GET["menu_id"];
$applicable = $_GET["applicable"];
list($gun,$ay,$yil) = explode('/',$applicable);
$yeni_tarih = $yil.'-'.$ay.'-'.$gun;
$price = $_GET["price"];
$priceId = $_GET["priceId"];

if(isset($_GET["priceId"])){
	$stmt ="UPDATE PRICE_LIST
				SET PRICE = ?, 
					APPLICABLE_FROM = ? 
				WHERE ID = ?";
	$statement = $db->prepare($stmt);
	$statement->bind_param('dsi', $price, $yeni_tarih, $priceId);
	if(!$statement->execute()){
		echo 'hata'.$menuId;
	}else{
		echo $menuId;
	}
	$statement->close();
}else{
	$version = 0;
	$stmt ="SELECT MAX(VERSION) AS VERSION FROM PRICE_LIST WHERE MENU_ID = $menuId";
	$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
	if($row = $res->fetch_assoc()){
		$version = $row['VERSION']+1;
		$res->close();
	}
	
	$stmt ="INSERT INTO PRICE_LIST (MENU_ID, VERSION, PRICE, APPLICABLE_FROM) VALUES(?,?,?,?)";
	$statement = $db->prepare($stmt);
	$statement->bind_param('iids', $menuId, $version, $price, $yeni_tarih);
	if(!$statement->execute()){
		echo 'hata'.$menuId;
	}else{
		echo $menuId;
	}
	$statement->close();
}
disconnect($db);
?>
<?php ob_end_flush(); ?>